package com.ruoyi.web.controller.company.notice;

import java.io.IOException;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.company.notice.domain.ComNotice;
import com.ruoyi.company.notice.service.IComNoticeService;
import com.ruoyi.company.uploadfile.domain.SysFileInfo;
import com.ruoyi.company.uploadfile.service.ISysFileInfoService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公告管理Controller
 * 
 * @author baike
 * @date 2020-12-18
 */
@Controller
@RequestMapping("/company/notice")
public class ComNoticeController extends BaseController
{
    private String prefix = "company/notice";

    @Autowired
    private IComNoticeService comNoticeService;
    
    @Autowired
    private ISysFileInfoService sysFileInfoService;

    @RequiresPermissions("company:notice:view")
    @GetMapping()
    public String notice()
    {
        return prefix + "/notice";
    }

    /**
     * 查询公告管理列表
     */
    @RequiresPermissions("company:notice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ComNotice comNotice)
    {
        startPage();
        List<ComNotice> list = comNoticeService.selectComNoticeList(comNotice);
        return getDataTable(list);
    }

    /**
     * 导出公告管理列表
     */
    @RequiresPermissions("company:notice:export")
    @Log(title = "公告管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ComNotice comNotice)
    {
        List<ComNotice> list = comNoticeService.selectComNoticeList(comNotice);
        ExcelUtil<ComNotice> util = new ExcelUtil<ComNotice>(ComNotice.class);
        return util.exportExcel(list, "notice");
    }

    /**
     * 新增公告管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存公告管理
     */
    @RequiresPermissions("company:notice:add")
    @Log(title = "公告管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ComNotice comNotice)
    {
    	comNotice.setNoticeId(IdUtils.simpleUUID());
    	comNotice.setCreateUser(ShiroUtils.getLoginName());
        return toAjax(comNoticeService.insertComNotice(comNotice));
    }

    /**
     * 修改公告管理
     */
    @GetMapping("/edit/{noticeId}")
    public String edit(@PathVariable("noticeId") String noticeId, ModelMap mmap)
    {
        ComNotice comNotice = comNoticeService.selectComNoticeById(noticeId);
        mmap.put("comNotice", comNotice);
        return prefix + "/edit";
    }

    /**
     * 修改保存公告管理
     */
    @RequiresPermissions("company:notice:edit")
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ComNotice comNotice)
    {
        return toAjax(comNoticeService.updateComNotice(comNotice));
    }

    /**
     * 删除公告管理
     */
    @RequiresPermissions("company:notice:remove")
    @Log(title = "公告管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(comNoticeService.deleteComNoticeByIds(ids));
    }
    
    @RequiresPermissions("company:notice:upload")
    @GetMapping("/upload/{fileId}/{model}")
    public String upload(@PathVariable("fileId") String fileId,@PathVariable("model") String model, ModelMap mmap)
    {
    	mmap.put("fileId", fileId);
    	mmap.put("model", model);
        return prefix + "/upload";
    }

    /**
     * 新增保存文件信息
     * @throws IOException 
     */
    //@RequiresPermissions("company:uploadfile:add")
    @Log(title = "文件信息", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file,@RequestParam(value="fileId") String fileId,@RequestParam(value="model") String model,SysFileInfo sysFileInfo) throws IOException
    {
    	//获取源文件名称
    	String originalFilename = StringUtils.substringBefore(file.getOriginalFilename(), ".");
    	//获取文件类型
    	String fileType = StringUtils.substringAfter(file.getOriginalFilename(), ".");
    	//获取当前用户
    	String createUser = ShiroUtils.getLoginName();
    	//上传文件路径
    	String filePath = RuoYiConfig.getUploadPath();
    	// 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        sysFileInfo.setFilePath(fileName);
        sysFileInfo.setRelationId(fileId);
        sysFileInfo.setModelName(model);
        sysFileInfo.setFileType(fileType);
        sysFileInfo.setFileName(originalFilename);
        //sysFileInfo.setCreateUser(createUser);
    	sysFileInfo.setFileId(IdUtils.simpleUUID());
        return toAjax(sysFileInfoService.insertSysFileInfo(sysFileInfo));
    }
}
